import pymysql
import pymysql.cursors
import sys
import json
from decimal import *
def db():
    dbconn=pymysql.connect(
    host="127.0.0.1",
    database="test",
    user="root",
    password="",
    port=3306,
    charset='utf8'
    )
    return dbconn
def sele_mouth(month,date,comp_date):
    dbconn = db()
    cursor = dbconn.cursor()
    cursor1 = dbconn.cursor()
    sql = "SELECT  MONTH(createdate) AS months,COUNT( DISTINCT orderid)/(SELECT  COUNT( DISTINCT createdate) FROM all_data WHERE YEAR(createdate)=2016 and MONTH(createdate)='%s')  AS orderSum,COUNT(DISTINCT freightId)/(SELECT  COUNT( DISTINCT createdate) FROM all_data WHERE YEAR(createdate)=2016 and MONTH(createdate)='%s') as freiSum,COUNT(DISTINCT bourn)/(SELECT  COUNT( DISTINCT createdate) FROM all_data WHERE YEAR(createdate)=2016 and MONTH(createdate)='%s') as bournSum FROM all_data WHERE YEAR(createdate)=2016 and MONTH(createdate)='%s' GROUP BY MONTH(createdate)" % (month,month,month,month) 
    hql = "SELECT  COUNT(DISTINCT orderid),COUNT(DISTINCT bourn) FROM all_data WHERE createdate = '%s' union SELECT COUNT(DISTINCT orderid),COUNT(DISTINCT bourn) FROM all_data WHERE createdate = '%s'" % (date,comp_date)
    cursor.execute(sql)
    cursor1.execute(hql)
    myresult = cursor.fetchall()
    myresult1 = cursor1.fetchall()
    print(str(myresult[0][1].quantize(Decimal('0.0'))))
    print(str(myresult[0][2].quantize(Decimal('0.0'))))
    print(str(myresult[0][3].quantize(Decimal('0.0'))))
    print(str(myresult1[0][0]))
    print(str(myresult1[0][1]))
    print(str(myresult1[1][0]))
    print(str(myresult1[1][1]))
month = sys.argv[1]
date = sys.argv[2]
comp_date = sys.argv[3]
# month = 4 
sele_mouth(month,date,comp_date)